package com.hst.capacity.infra.persistence.jpa.marketing.coupon;


import com.hst.capacity.domain.model.entity.marketing.coupon.CouponInfoEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

public interface CouponInfoRepository extends JpaRepository<CouponInfoEntity, String> {

    @Query("select t from CouponInfoEntity t where t.idFranchisee = :idFranchisee")
    CouponInfoEntity findByIdFranchisee(String idFranchisee);

    //CouponInfoEntity findByIdMember(String idMember);

    @Query("select t from CouponInfoEntity t where t.couponCode = :couponCode And t.isUsed = 0")
    CouponInfoEntity findByCouponCode(String couponCode);

    Page<CouponInfoEntity> findByIdMember(String idMember, Pageable pageable);

    Page<CouponInfoEntity> findByIdMemberAndIsUsed(String idMember, int isUsed, Pageable pageable);

    @Query("select t from CouponInfoEntity t where t.idMember = :idMember")
    List<CouponInfoEntity> findListByIdMember(String idMember);

    /**
     * 根据下单人手机查找有效优惠券 todo 条件限定
     * @param idMember
     * @return
     */
    @Query("select t from CouponInfoEntity t where t.idMember=:idMember and t.isUsed =0 and :today between t.effectiveTime and t.expiredTime order by t.couponDiscount,t.couponDiscountRate desc")
    List<CouponInfoEntity> findListByIdMember(@Param("idMember") String idMember, @Param("today") LocalDateTime today);

    /**
     * 根据下单人手机查找有效优惠券 todo 条件限定
     * @param idMember
     * @return
     */
    @Query("select t from CouponInfoEntity t where t.idMember=:idMember and t.isUsed =0 and t.couponType =:couponType and :today between t.effectiveTime and t.expiredTime order by t.couponDiscount,t.couponDiscountRate desc")
    List<CouponInfoEntity> findListByIdMember(@Param("idMember") String idMember, @Param("couponType") int couponType, @Param("today") LocalDateTime today);

    /**
     * 根据下单人手机查找有效优惠券 todo 条件限定 and t.couponType = 1
     * @param idMember
     * @return
     */
    @Query("select t from CouponInfoEntity t where t.idMember = :idMember and t.isUsed = 0 and t.idFranchisee = :idFranchisee and :today between t.effectiveTime and t.expiredTime order by t.couponDiscount,t.couponDiscountRate desc")
    List<CouponInfoEntity> findListByIdMember(@Param("idMember") String idMember, @Param("idFranchisee") String idFranchisee, @Param("today") LocalDateTime today);

    /**
     * 优惠券已使用
     * @param isUsed
     * @param couponCode
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    @Query("update CouponInfoEntity t set t.isUsed = :isUsed where t.couponCode = :couponCode")
    int updateIsUsed(int isUsed, String couponCode);

}